//
//  FSHudManager.m
//  FSiOSClient
//
//  Created by purlible on 2024/8/22.
//

#import "FSHudManager.h"
#import "FSUIMarco.h"

@interface FSHudManager ()

@property(nonatomic, weak) MBProgressHUD *hud;

@end

@implementation FSHudManager

static FSHudManager *sharedInstance_ = nil;


+(instancetype) sharedInstance
{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance_ = [[self alloc] init];
    });
    return sharedInstance_;
}

- (void)showText:(NSString *)text view:(id)view
{
    [self showHUDWithImage:nil withText:text view:view];
    [self hideDelay];
}

- (void)showLoadingHUD:(UIView*)view text:(NSString*)text{
    
    [self showHUDWithImage:@"icon_zn_loading" withText:text view:view];

}

- (void) hideDelay
{
    [self performSelector:@selector(hideHUD) withObject:nil afterDelay:2];
}

- (void)showSuccess:(NSString *)text inView:(UIView*)view
{
    [self showHUDWithImage:@"ZNVerifySource.bundle/icon_zn_sucess"  withText:text view:view];
    [self hideDelay];
}

- (void)showError:(NSString *)text inView:(UIView*)view
{
    [self showHUDWithImage:@"ZNVerifySource.bundle/icon_zn_failed"  withText:text view:view];
    [self hideDelay];
}

- (void)showHUDWithImage:(NSString*)imgName withText:(NSString *)text view:(UIView*)view
{
    if(self.hud)
    {
        [self hideHUD];
    }
    
    MBProgressHUD* HUD = [[MBProgressHUD alloc] initWithView:view];
    if (imgName == nil)
    {
        HUD.mode = MBProgressHUDModeText;
        HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    }
    else
    {
        HUD.mode = MBProgressHUDModeCustomView;
        HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        HUD.animationType = MBProgressHUDAnimationZoomIn;
        
        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
        UIImage *loadingImage = [UIImage imageNamed:imgName inBundle:bundle compatibleWithTraitCollection:nil];
        
        UIImageView *iconImageView = [[UIImageView alloc] initWithImage:loadingImage];
        
        if ([imgName containsString:@"loading"])
        {
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
            //默认是顺时针效果，若将fromValue和toValue的值互换，则为逆时针效果
            animation.fromValue = [NSNumber numberWithFloat:0.f];
            animation.toValue = [NSNumber numberWithFloat: M_PI *2];
            animation.duration = 1;
            animation.autoreverses = NO;
            animation.fillMode = kCAFillModeForwards;
            animation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
            [iconImageView.layer addAnimation:animation forKey:nil];
        }
        
        HUD.customView = iconImageView;
    }

    HUD.bezelView.color = [[UIColor grayColor] colorWithAlphaComponent:1];
    HUD.label.textColor = [UIColor whiteColor];
    HUD.label.font = FONT_SIZE_15;
    HUD.detailsLabel.textColor = [UIColor whiteColor];
    HUD.detailsLabel.font = FONT_SIZE_15;
    HUD.margin = scale_by_screen_width(20);
    HUD.offset = CGPointMake(0, -100);

    [HUD.customView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.scale_offset(31);
        make.height.scale_offset(31);
    }];

    
    [view addSubview:HUD];
    self.hud = HUD;
    
    if(!isNull(text))
    {
        if(text.length<=12)
        {
            HUD.label.text = text;
        }
        else
        {
            HUD.detailsLabel.text = text;
        }
    }
    
    HUD.removeFromSuperViewOnHide = YES;
    [HUD showAnimated:YES];
}

- (void)hideHUD
{
    if(!self.hud)
    {
        return;
    }
    
    [self.hud hideAnimated:YES];
    [self.hud removeFromSuperview];
    self.hud = nil;
}



@end
